<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */

class LovePetsUserIdentity extends CUserIdentity
{
    private $_id;
    
    public function authenticate()
    {
        $user = UserFacade::getUserByEmail($this->username);

        if ($user === null)
        {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        }
        else if (!UserFacade::isValidPassword($user, $this->password))
        {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        }
        else
        {
            $this->_id = $user->user_id;
            $this->setState('login', $user->email);
            $this->errorCode = self::ERROR_NONE;
        }
        if ($this->errorCode == self::ERROR_NONE)
        {
            return true;
        }
        return false;
    }

    public function getId()
    {
        return $this->_id;
    }
}